package com.lzb.compressmaster.activity;

import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.HomeActivity;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.basic.utils.FileUtil;
import com.lzb.compressmaster.databinding.ActivityInfoErasureBinding;
import com.lzb.compressmaster.utils.PhotoPrivacyUtils;
import com.lzb.compressmaster.utils.RxUtil;

import io.reactivex.rxjava3.core.ObservableEmitter;

public class InfoErasureActivity extends BaseAc<ActivityInfoErasureBinding> implements View.OnClickListener {

    public static String sPath;
    private PhotoPrivacyUtils.PhotoPrivacyInfo privacyInfo;

    @Override
    protected int onCreate() {
        return R.layout.activity_info_erasure;
    }

    @Override
    protected void initView() {
        mDataBinding.ivInfoErasureBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvErasureInfo.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvErasureInfo) {//一键抹除敏感信息
            erasureInfo();
        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(sPath).into(mDataBinding.ivImg);
        getData();
    }

    private void getData() {
        privacyInfo = PhotoPrivacyUtils.getSensitiveInfo(sPath);
        if (privacyInfo == null) {
            mDataBinding.tvCameraModel.setText(getString(R.string.no_info));
            mDataBinding.tvShootDate.setText(getString(R.string.no_info));
            mDataBinding.tvCameraManufacturer.setText(getString(R.string.no_info));
            mDataBinding.tvFlashlight.setText(getString(R.string.no_info));
        } else {
            mDataBinding.tvCameraModel.setText(privacyInfo.cameraModel == null ? getString(R.string.no_info) : privacyInfo.cameraModel);
            mDataBinding.tvShootDate.setText(privacyInfo.shootDate == null ? getString(R.string.no_info) : privacyInfo.shootDate);
            mDataBinding.tvCameraManufacturer.setText(privacyInfo.cameraMaker == null ? getString(R.string.no_info) : privacyInfo.cameraMaker);
            mDataBinding.tvFlashlight.setText(privacyInfo.flashFired ? getString(R.string.on) : getString(R.string.off));
        }
    }

    //抹除信息
    private void erasureInfo() {
        showDialog(getString(R.string.erasure_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        String path = FileUtil.generateFilePath(PathUtils.getFilesPathExternalFirst() + "/work/img", "." + FileUtils.getFileExtension(sPath));
                        PhotoPrivacyUtils.eraseSensitiveInfo(sPath, null);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.erasure_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

}